package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.common.base.CoreServiceImpl;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.dao.ResumesDao;
import com.jinzhi.jzweb.domain.ResumesDO;
import com.jinzhi.jzweb.domain.ResumesVO;
import com.jinzhi.jzweb.service.ResumesService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.jzweb.utils.HttpClientUtil;
import com.jinzhi.sys.dao.UserDao;
import com.jinzhi.sys.domain.UserDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 简历
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Service
public class ResumesServiceImpl extends CoreServiceImpl<ResumesDao, ResumesDO> implements ResumesService {

    @Autowired
    private ResumesDao resDao;

    @Autowired
    private UserDao userDao;

    @Override
    public void exportResumes(HttpServletRequest request, HttpServletResponse response, String[] rows, Integer pageNum) {
        List<ResumesDO> groupList = new ArrayList<>();
        if(rows.length > 0) {
            groupList =	this.selectBatchIds(Arrays.asList(rows));
            groupList = groupList.stream().sorted(Comparator.comparing(ResumesDO::getId).reversed()).collect(Collectors.toList());
        }else {
            groupList = resDao.selectExportList((pageNum-1)*300);
        }

        List<ResumesVO> excelList = new ArrayList<ResumesVO>();
        for(ResumesDO info : groupList) {
            ResumesVO excel = new ResumesVO();

            ResumesDO resumes = this.selectById(info.getId());
            if (resumes.getUserId() != null) {
                UserDO userDO = userDao.getByUserId(resumes.getUserId());
                if (userDO != null) {
                    if (StringUtil.notEmpty(userDO.getName())){
                        excel.setName(userDO.getName());
                    }else {
                        excel.setName(userDO.getUsername());
                    }
                    if (StringUtil.notEmpty(userDO.getMobile())){
                        excel.setMobile(userDO.getMobile());
                    }else {
                        excel.setMobile("");
                    }
                    if(userDO.getSex() == null){ // 性别
                        excel.setSex("");
                    }else if(userDO.getSex() == 2){
                        excel.setSex("女");
                    }else if(userDO.getSex() == 1){
                        excel.setSex("男");
                    }
                    if(StringUtil.notEmpty(userDO.getHeadPortrait())){
                        byte[] imageFromNetByUrl = HttpClientUtil.getImageFromNetByUrl(userDO.getHeadPortrait());
                        excel.setHeadPortrait(imageFromNetByUrl);
                    }
                    if(StringUtil.notEmpty(userDO.getPoliticalAffiliation())){
                        excel.setPoliticalAffiliation(userDO.getPoliticalAffiliation());
                    }
                    if(StringUtil.notEmpty(userDO.getLiveAddress())) {
                        excel.setLiveAddress(userDO.getLiveAddress());
                    }
                    if(StringUtil.notEmpty(userDO.getEmail())) {
                        excel.setEmail(userDO.getEmail());
                    }
                    if(StringUtil.notEmpty(userDO.getDateOfBirth())) {
                        excel.setDateOfBirth(userDO.getDateOfBirth());
                    }
                }
            }

            if(StringUtil.notEmpty(info.getNativePlace())) {
                excel.setNativePlace(info.getNativePlace());
            }
            if(StringUtil.notEmpty(info.getGraduatedFrom())) {
                excel.setGraduatedFrom(info.getGraduatedFrom());
            }
            //excel.setWorkExperience(info.getWorkExperience());
            if(StringUtil.notEmpty(info.getDesiredOccupation())) {
                excel.setDesiredOccupation(info.getDesiredOccupation());
            }
            if(StringUtil.notEmpty(info.getExpectedSalary())) {
                excel.setExpectedSalary(info.getExpectedSalary());
            }
            if(StringUtil.notEmpty(info.getWorkYears())) {
                excel.setWorkYears(info.getWorkYears());
            }
            if(StringUtil.notEmpty(info.getSelfEvaluation())) {
                excel.setSelfEvaluation(info.getSelfEvaluation());
            }

//            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            excel.setCreaterDate(formatter.format(info.getCreaterDate()));
//            excel.setUpdateDate(formatter.format(info.getUpdateDate()));
            //excel.setEducationExperience(info.getEducationExperience());

            excelList.add(excel);
        }
        EasyPoiUtil.exportExcel(excelList, "简历信息", "简历信息", ResumesVO.class, "简历信息表", response);
    }
}
